// pages/ordrer_details/ordrer_details.js
// 获取应用实例
const app = getApp()
var baseurl = app.baseurl.url;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    yuming: app.yuming.url,
    address: '',
    currentTab: '',
    order_id: '',
    flag: true,
    express_delivery: ''// 快递
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options.currentTab);
    // console.log(options.order_id);
    this.setData({
      currentTab: options.currentTab,
      order_id: options.order_id
    })
    this.address()
  },

  address() {
    wx.request({
      url: `${baseurl}/good.ExchangeShop/order_details`,
      method: 'POST',
      data: {
        order_id: this.data.order_id,
        user_id: wx.getStorageSync("user_id"),
      },
      success: (res) => {
        console.log(res.data.data);
        this.setData({
          address: res.data.data,
        })
        // this.info()
      }
    })
  },
  // 取消订单
  close() {
    wx.request({
      url: `${baseurl}/order.Order/order_esc`,
      data: {
        user_id: wx.getStorageSync("user_id"),
        order_id: this.data.order_id,
      },
      success: (res) => {
        // console.log('取消订单',res.data);
        // var appid = getApp().globalData.appid; //appid
        // console.log(appid);

        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 2000
        })
        setTimeout(() => {
          wx.reLaunch({
            url: '/pages/order/order',
          })
        }, 600);
      }
    })
  },
  // 去支付
  pay() {
    // 待支付订单支付
    wx.request({
      url: `${baseurl}/order.Order/orderpay`,
      method:'POST',
      data: {
        total_price: this.data.address.cash_price,
        user_id: wx.getStorageSync("user_id"),
        openid: wx.getStorageSync("openid"),
        order_id: this.data.order_id,
        province:this.data.address.province
      },
      success: (res) => {
        console.log('待支付订单', res.data);
        // var appid = getApp().globalData.appid; //appid
        // console.log(appid);
        let timeStamp = res.data.data.data_pay_sign.timeStamp
        let nonceStr = res.data.data.data_pay_sign.nonceStr
        let pack = res.data.data.data_pay_sign.package
        let paySign = res.data.data.data_pay_sign.paySign
        let signType = res.data.data.data_pay_sign.signType
        wx.requestPayment(
          {
            "timeStamp": timeStamp,
            "nonceStr": nonceStr,
            "package": pack,
            "signType": signType,
            "paySign": paySign,
            "success": function (res) {
              // console.log('111', res);
              wx.showToast({
                title: '支付成功',
                // icon: 'none',
                duration: 2000
              })
              // 支付成功
              wx.navigateTo({
                url: '/pages/success/success',
              })
            },
            "fail": function (res) {
              // console.log('222', res);
              wx.showToast({
                title: '支付失败',
                icon: 'none',
                duration: 2000
              })
            },
            "complete": function (res) {
              // 接口调用结束的回调函数（调用成功、失败都会执行）

            }
          })
      }
    })

  },
  click() {
    this.setData({
      flag: false
    })
  },
  info() {
    this.setData({
      flag: false
    })
  },
  mark(){
    this.setData({
      flag: true
    })
  },
  // 快递物流
  info() {
    wx.request({
      url: `${baseurl}/order.Order/findorder`,
      method: 'POST',
      data: {
        user_id: wx.getStorageSync("user_id"),
        express_no: this.data.address.express_no,
        phone: this.data.address.phone.substring(7, 11)
      },
      success: (res) => {
        // console.log(res.data.data.msgData.routeResps[0].routes);
        this.setData({
          express_delivery: res.data.data.msgData.routeResps
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

})